package com.demo.product.controller;

import com.demo.product.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商品控制器
 * 提供商品相关的API接口
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    private static final Logger log = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private SeckillService seckillService;

    /**
     * 获取商品库存
     * @param productId 商品ID
     * @return 库存信息
     */
    @GetMapping("/stock/{productId}")
    public Map<String, Object> getStock(@PathVariable Long productId) {
        log.info("获取商品库存，productId: {}", productId);
        
        try {
            // 参数校验
            if (productId == null || productId <= 0) {
                return Map.of(
                        "success", false,
                        "message", "商品ID无效"
                );
            }
            
            // 获取库存
            Integer stock = seckillService.getCurrentStock(productId);
            return Map.of(
                    "success", true,
                    "message", "查询成功",
                    "productId", productId,
                    "stock", stock
            );
            
        } catch (Exception e) {
            log.error("获取库存异常，productId: {}", productId, e);
            return Map.of(
                    "success", false,
                    "message", "查询失败"
            );
        }
    }

    /**
     * 初始化商品库存
     * @param productId 商品ID
     * @param stock 库存数量
     * @return 初始化结果
     */
    @PostMapping("/init-stock")
    public Map<String, Object> initStock(@RequestParam Long productId, 
                                        @RequestParam Integer stock) {
        log.info("初始化商品库存，productId: {}, stock: {}", productId, stock);
        
        try {
            // 参数校验
            if (productId == null || productId <= 0) {
                return Map.of(
                        "success", false,
                        "message", "商品ID无效"
                );
            }
            
            if (stock == null || stock <= 0) {
                return Map.of(
                        "success", false,
                        "message", "库存数量必须大于0"
                );
            }
            
            // 初始化库存
            seckillService.initStock(productId, stock);
            return Map.of(
                    "success", true,
                    "message", "库存初始化成功",
                    "productId", productId,
                    "stock", stock
            );
            
        } catch (Exception e) {
            log.error("初始化库存异常，productId: {}, stock: {}", productId, stock, e);
            return Map.of(
                    "success", false,
                    "message", "初始化失败"
            );
        }
    }

    /**
     * 获取商品信息
     * @param productId 商品ID
     * @return 商品信息
     */
    @PostMapping("/info/{productId}")
    public Map<String, Object> getProductInfo(@PathVariable Long productId) {
        log.info("获取商品信息，productId: {}", productId);
        
        try {
            // 参数校验
            if (productId == null || productId <= 0) {
                return Map.of(
                        "success", false,
                        "message", "商品ID无效"
                );
            }
            
            // 这里应该从数据库查询商品信息，暂时返回模拟数据
            return Map.of(
                    "success", true,
                    "message", "查询成功",
                    "productId", productId,
                    "productName", "秒杀商品" + productId,
                    "price", 99.99,
                    "description", "这是一个秒杀商品"
            );
            
        } catch (Exception e) {
            log.error("获取商品信息异常，productId: {}", productId, e);
            return Map.of(
                    "success", false,
                    "message", "查询失败"
            );
        }
    }
}